﻿Imports System.ComponentModel
Imports System.Collections.ObjectModel

''' <summary>
''' Represents an additional content for Visual Studio
''' </summary>
''' <remarks></remarks>
Public Class ItemFile


    ''' <summary>
    ''' Gets or sets the full pathname of the additional content
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property FullPath As String

    ''' <summary>
    ''' The name of the file to install
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property FileName() As String

    ''' <summary>
    ''' If the item is an add-in, this holds the name of the .addin file
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Tag As String = String.Empty

    ''' <summary>
    ''' Describes the content inside Visual Studio
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Description() As String

    ''' <summary>
    ''' Describes the content during the installation procedure
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property DisplayName() As String

    ''' <summary>
    ''' The contenty type (e.g. snippet, add-in)
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property FileContentType() As String

    ''' <summary>
    ''' Indicates the version of Visual Studio that will receive the additional content.
    ''' It can be 1.0 (VS 2005) or 2.0 (VS 2005/2008/2010)
    ''' Always use 2.0 with VS 2010
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property ContentVersion() As String

    ''' <summary>
    ''' Attribute collection for the current item
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Attributes() As Dictionary(Of String, String)

    Public Sub New(ByVal fullPathName As String)
        Me.Attributes = New Dictionary(Of String, String)
        Me.FullPath = fullPathName
        Me.FileName = IO.Path.GetFileName(fullPathName)
    End Sub

End Class

''' <summary>
''' Represents a list of contents for Visual Studio
''' </summary>
''' <remarks>Vsi Builder 2010 supports only code snippets and add-ins</remarks>
Public Class ItemsList
    Inherits ObservableCollection(Of ItemFile)

    ''' <summary>
    ''' Saves the list of items in the collection as an XML file for later reuse
    ''' </summary>
    ''' <param name="fileName"></param>
    ''' <remarks></remarks>
    Public Sub SaveToXml(ByVal fileName As String)
        Try

            Dim dox As XDocument = <?xml version="1.0"?>
                                   <ItemsList>
                                       <%= From item In Me
                                           Select <Item FileName=<%= item.FileName %> FileContentType=<%= item.FileContentType %> FullPath=<%= item.FullPath %>>
                                                  </Item>
                                       %>
                                   </ItemsList>

            dox.Save(fileName)
        Catch ex As Exception
            Throw
        End Try

    End Sub

    ''' <summary>
    ''' Loads an existing list of items previously saved with the Save method
    ''' and imports all the items in the current instance of the collection
    ''' </summary>
    ''' <param name="fileName"></param>
    ''' <remarks></remarks>
    Public Sub LoadFromXml(ByVal fileName As String)
        Try
            Dim dox As XDocument = XDocument.Load(fileName)

            Dim query = From xmlItem In dox...<Item>
                        Select New ItemFile(xmlItem.@FullPath) With {.FileName = xmlItem.@FileName, .FileContentType = xmlItem.@FileContentType}

            Me.Clear()
            For Each element In query
                Me.Add(element)
            Next
            Exit Sub
        Catch ex As Exception
            Throw
        End Try

    End Sub
End Class